/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.com.jsepc.rep.setvalue;

import cn.com.jsepc.rep.comm.ConstUtil;
import cn.com.jsepc.rep.report.ImportXls;
import cn.com.jsepc.rep.vo.RepDataVO;
import cn.com.jsepc.rep.vo.RepDemensionVO;
import com.jarp.db.DAO;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import net.sf.json.JSONArray;

/**
 *
 * @author 赵远
 * @time 2013-6-14
 */
public class SetValueDAO {

    private DAO dao = new DAO();

    /**
     * 获取定时任务的详细列表
     *
     * @return
     */
    public String getGridData(String repid, String pageCode, String pageSize) {
        int iStart = Integer.parseInt(pageCode);
        int iPageSize = Integer.parseInt(pageSize);
        int iCurPage = iStart / iPageSize + 1;
        String res = "";
        StringBuffer sqlBuf = new StringBuffer();   //拼接SQL语句
        JSONArray json = new JSONArray();
        ArrayList colList = new ArrayList();   //记录该指标有哪些维度
        sqlBuf.append("SELECT a.ID,a.REPID,a.REFACT,b.REPNAME,a.ROLEID,a.TIME,a.REPVALUE,a.SUMVALUE,a.TREPVAL,a.TSUMVAL,a.PREVAL,a.TPREVAL,a.ADDTIME,a.STARTTIME,a.ENDTIME");
        RepDataVO repVo = new ImportXls().getRepDemension(repid);
        String sql = "SELECT * FROM TB_SETVALUE t WHERE t.REPID=" + repid;
        try {
            Vector v = dao.getDataSet(ConstUtil.DBNAME, sql);
            if (v != null) {
                Iterator<Hashtable> iterator = v.iterator();
                while (iterator.hasNext()) {
                    Hashtable hash = iterator.next();
                    ArrayList<RepDemensionVO> repDemLst = repVo.getRepDemLst();
                    if (repVo != null) {
                        for (int j = 0; j < repDemLst.size(); j++) {
                            RepDemensionVO repDemVO = repDemLst.get(j);
                            for (int i = 1; i <= 7; i++) {
                                if (hash.get("DIM" + i).equals(repDemVO.getFactdimcol())) {
                                    sqlBuf.append("," + ("a.DIMVALUE" + i) + " as " + hash.get("DIM" + i));
                                    sqlBuf.append("," + ("a.DIMVALUEWAN" + i) + " as " + hash.get("DIMWAN" + i) + "_WAN");
                                }
                            }
                        }
                    }
                }
            } else {   //该指标没有任务数据
            }
        } catch (ServletException ex) {
            Logger.getLogger(SetValueDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        sqlBuf.append(" from tb_setvalue a ");
        sqlBuf.append(" left join tb_report b on a.repid=b.repid ");
        sqlBuf.append("where a.repid=" + repid);
        try {
            Vector v2 = dao.getDataSet(ConstUtil.DBNAME, sqlBuf.toString(), iCurPage, iPageSize);
            int totalCount = 0;
            if (v2 != null) {
                totalCount = dao.getDataSetCount(ConstUtil.DBNAME, sqlBuf.toString());
                json = JSONArray.fromObject(v2);
            }
            res = "{totalCount:" + totalCount + ",data:" + json.toString().toLowerCase()
                    + "}";
        } catch (ServletException ex) {
            Logger.getLogger(SetValueDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }
    
    /**
     * 删除某一定时调度任务
     * @param id
     * @return 
     */
    public String deleteFunction(String id){
        String res="";
        String sql="DELETE FROM TB_SETVALUE t WHERE t.ID="+id;
        try {
            int count=dao.execCommand(ConstUtil.DBNAME, sql);
            if(count>0){
                res="任务删除成功！";
            }else{
                res="任务删除失败！";
            }
        } catch (ServletException ex) {
            res="系统错误！";
            Logger.getLogger(SetValueDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }
}
